﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using ProjectN.Core.Fore.Editor;
using ProjectN.Core.Instance.Data;

namespace ProjectN.Core.Element.Def
{
    public interface IDeclaration<out T> where T : notnull
    {
        public T Create();
    }

    [JsonObject(MemberSerialization.OptIn)]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class PenInfo : DataSpreader, IDeclaration<Pen>
    {
        [DisplayName("笔刷")]
        [JsonProperty] public BrushInfo Brush { get; set; } = new();

        [DisplayName("宽度")]
        [JsonProperty] public int Width { get; set; } = 1;

        public Pen Create() => new(Brush.Create(), Width);

        protected override void UpdateData(InstanceData data)
        {
            Brush.Data = data;
        }

        public override string ToString() => $"宽度：{Width}像素";
    }

    [JsonObject(MemberSerialization.OptIn)]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class BrushInfo : DataSpreader, IDeclaration<Brush>
    {
        [Browsable(false), DisplayName("是否为纹理笔刷")]
        [JsonProperty] public bool ImageMode { get; set; } = false;

        [DisplayName("颜色")]
        [Editor(typeof(ColorEditor), typeof(UITypeEditor))]
        [JsonProperty] public Color? Color { get; set; } = System.Drawing.Color.Black;

        [Browsable(false), DisplayName("纹理")]
        public ImageInfo? Image { get; set; }

        public Brush Create()
        {
            if (ImageMode)
            {
                if (Image == null)
                    throw new InvalidOperationException("Image not specified.");
                return new TextureBrush(Image.Create());
            }
            if (Color == null)
                throw new InvalidOperationException("Color not specified.");
            return new SolidBrush(Color.Value);
        }

        protected override void UpdateData(InstanceData data)
        {
            if (Image != null)
                Image.Data = data;
        }
        public override string ToString() => $"纯色笔刷";
    }

    [JsonObject(MemberSerialization.OptIn)]
    [Editor(typeof(FontInfo_EditorDEF), typeof(UITypeEditor))]
    public class FontInfo : IDeclaration<Font>
    {
        public static readonly FontInfo Default = new() { FamilyName = "微软雅黑", Size = 14, Style = FontStyle.Regular };

        [JsonProperty] public required string FamilyName { get; set; }
        [JsonProperty] public required float Size { get; set; }
        [JsonProperty] public required FontStyle Style { get; set; }

        public Font Create() => new(FamilyName, Size, Style);
        public override string ToString() => $"{Size}像素：{FamilyName}";
    }

    [JsonObject(MemberSerialization.OptIn)]
    [Editor(typeof(ImageInfo_EditorDEF), typeof(UITypeEditor))]
    public class ImageInfo : DataReceiver, IDeclaration<Image>
    {

        [JsonProperty] public string Identity { get; set; } = string.Empty;

        public Image Create()
        {
            //var handler = Data.ImageCollection.GetHandler(Identity)
            //              ?? throw new InvalidOperationException("Image not found.");
            return Data.ImageCollection.GetHandler(Identity)?.Image
                    ?? new Bitmap(1, 1);
        }
        public override string ToString() => $"{Identity}";
    }
}
